package com.lzs.bootsafety.business.sys.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lzs.bootsafety.business.sys.entity.SysRole;
import com.lzs.bootsafety.business.sys.service.SysRoleMenuService;
import com.lzs.bootsafety.common.base.controller.BaseController;
import com.lzs.bootsafety.common.util.Resu;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/sys/role")
public class RoleController extends BaseController {
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @GetMapping("/getRoleList")
    public Resu getList(){
        return Resu.suces().put("data",sysRoleService.getRoleList());
    }
    @GetMapping("/list")
    @RequiresPermissions("sys:role:list")
    public  Resu list(@RequestParam("data") String data){
        SysRole sysRole = JSON.parseObject(data, SysRole.class);
        PageInfo<SysRole> pageInfo = sysRoleService.getRoleByPage(sysRole);
        return  Resu.page(pageInfo.getList(),pageInfo.getTotal(),200);
    }
    @PostMapping("/add")
    @RequiresPermissions("sys:role:add")
    public Resu add(@RequestParam("data") String data){
        SysRole sysRole = JSON.parseObject(data,SysRole.class);
        sysRole.setCreatePeople(getUserName());
        sysRole.setCreateTime(new Date());
        return respAdd(sysRoleService.addRole(sysRole));
    }
    @GetMapping("/info")
    @RequiresPermissions("sys:role:info")
    public Resu info(long roleId){
        Resu resu = Resu.suces();
        if (!StringUtils.isEmpty(roleId)){
            resu.put("role",sysRoleService.getRoleByRoleId(roleId));
            resu.put("menuIds",sysRoleMenuService.getMenuByRoleId(roleId));
        }
        return resu;
    }
    @PutMapping("/modify")
    @RequiresPermissions("sys:role:modify")
    public Resu modify(@RequestParam("data") String data){
        SysRole sysRole = JSON.parseObject(data,SysRole.class);
        sysRole.setUpdatePeople(getUserName());
        sysRole.setUpdateTime(new Date());
        return respudp(sysRoleService.modifyByPrimaryKeySelective(sysRole));
    }
    @DeleteMapping("/del")
    @RequiresPermissions("sys:role:del")
    public Resu del(Long[] roleIds){
        return respDel(sysRoleService.batchDelByPrimaryKey(roleIds));
    }
}
